﻿using System;
using System.ComponentModel;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class AB50LS : UserControl
    {
        VABPOS _AB;
        public VABPOS AB
        {
            get
            {
                if (_AB == null) _AB = new VABPOS();
                return _AB;
            }
        }
        VGAAUF _GD;
        public VGAAUF GD
        {
            get
            {
                if (_GD == null) _GD = new VGAAUF();
                return _GD;
            }
        }
        CollectionViewSource vwMU
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwMU");
            }
        }
        DataRow dwMU
        {
            get
            {
                if (vwMU.View == null)
                    return null;
                if ((DataRowView)vwMU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMU.View.CurrentItem).Row;
            }
        }
        DataRow dwABPOS;
        string filter = "";
        DataRow[] rows;
        DataTable tb;
        CollectionViewSource vwAPZUORD
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwAPZUORD");
            }
        }
        DataRow dwAPZUORD
        {
            get
            {
                if (vwAPZUORD.View == null)
                    return null;
                if ((DataRowView)vwAPZUORD.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwAPZUORD.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwTGAAUF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTGAAUF");
            }
        }
        DataRow dwTGAAUF
        {
            get
            {
                if (vwTGAAUF.View == null)
                    return null;
                if ((DataRowView)vwTGAAUF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTGAAUF.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwTZUTEI
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTZUTEI");
            }
        }
        DataRow dwTZUTEI
        {
            get
            {
                if (vwTZUTEI.View == null)
                    return null;
                if ((DataRowView)vwTZUTEI.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTZUTEI.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwGAAUF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGAAUF");
            }
        }
        DataRow dwGAAUF
        {
            get
            {
                if (vwGAAUF.View == null)
                    return null;
                if ((DataRowView)vwGAAUF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGAAUF.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwYARNA
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwYARNA");
            }
        }
        DataRow dwYARNA
        {
            get
            {
                if (vwYARNA.View == null)
                    return null;
                if ((DataRowView)vwYARNA.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwYARNA.View.CurrentItem).Row;
            }
        }
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        bool IsLoaded = false;
        SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        }

        public string KRS
        {
            get
            {

                return vm.KRS;
            }
            set
            {
                vm.KRS = value;
            }

        }
        public string LFD
        {
            get
            {
                return vm.LFD;
            }
            set
            {
                vm.LFD = value;
            }
        }
        public string ABKKDAUFTNR
        {
            get
            {
                return KRS.Trim() + "-" + Helpers.Helper.Right(LFD, 4).Trim();
            }
        }  /**/
        MainViewModel vm;
        public double TargetRatio
        {
            get
            {
                double value = Convert.ToDouble(txtAddRatio.Text);
                return value;
            }
            set
            {
                //2.设定TargetRatio，ABPSTKLG=ABPSOLLMENGE7*TargetRatio/ ABPSOLLMENGE5
                txtAddRatio.Text = value.ToString();
                dwGAAUF["ABPSTKLG"] = Math.Ceiling(value * 10000000);
            }
        }
        BindingListCollectionView cvGAAUF
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.dlgGAAUF.ItemsSource);
            }
        }
        public AB50LS()
        {

            InitializeComponent();
            this.Loaded += Windows_Loaded;
        }
        #region===留位========
        #region=====  vwGLLG=======
        CollectionViewSource vwGLLG
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGLLG");
            }
        }
        DataRow dwGLLG
        {
            get
            {
                if (vwGLLG.View == null)
                    return null;
                if ((DataRowView)vwGLLG.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGLLG.View.CurrentItem).Row;
            }
        }
        #endregion
        void Windows_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            IsLoaded = true;
            if (vm == null)
            {
                vm = (MainViewModel)this.DataContext;
            }
            vwGAAUF.View.CurrentChanged += vwGAAUF_CurrentChanged;
            vwYARNA.View.CurrentChanged += vwYARNA_CurrentChanged;
            cvGAAUF.CustomFilter = "GAKG2=0";
            if (app.User.Trim() == "JLTV")
            {
                //KRS = "J4";
                //LFD = "001539";
            }
            EventDispatcher.Instance.OnStatusChange += Instance_OnStatusChange;
        }
        void Instance_OnStatusChange(object sender, StatusEventArgs e)
        {
            MessageInfo msg = ((StatusEventArgs)e).Info;
            string Message = msg.Details.Trim();
            if (Message != "")
            {
                txtMessage.Text = msg.Details;
                if (Message.Contains("JLTAV95"))
                {
                    txtMessage.Visibility = Visibility.Visible;
                }
                if (Message.Contains("JLTAV95启动"))
                {
                    txtMessage.Visibility = Visibility.Visible;
                }
                if (Message.Contains("JLTAV95完成"))
                {
                    txtMessage.Visibility = Visibility.Collapsed;
                }
                if (Message.Contains("JLTAV95异常终止"))
                {
                    txtMessage.Visibility = Visibility.Collapsed;
                }

            }
        }
        void vwGAAUF_CurrentChanged(object sender, EventArgs e)
        {
            SetMaster.GLLG.Clear();
            SetMaster.ZUTEI.Clear();
            if (dwGAAUF == null)
                return;
            double GAKG1 = Convert.ToDouble(dwGAAUF["GAKG1"].ToString());
            double GAKG2 = Convert.ToDouble(dwGAAUF["GAKG2"].ToString());
            double GAKG3 = Convert.ToDouble(dwGAAUF["GAKG3"].ToString());
            double GAKG5 = Convert.ToDouble(dwGAAUF["GAKG5"].ToString());
            double REQ = (GAKG1 - GAKG2 - GAKG3 - GAKG5) / 100;
            txtRestReq.Text = REQ.ToString();
            if (REQ > 0)
            {
                btnBook.IsEnabled = true;
                btnRemoveBook.IsEnabled = false;
            }
            else
            {
                btnBook.IsEnabled = false;
                btnRemoveBook.IsEnabled = true;
            }

            DataSet SResult;
            DataRow dwData = SetDetail.ICC_QUERYTB.NewRow();
            try
            {
                dwData["ZUMANDANT"] = dwGAAUF["GAMANDANT"];
                dwData["ZUAUFKRS"] = dwGAAUF["GAAUFKRS"];
                dwData["ZUAUFLFD"] = dwGAAUF["GAAUFLFD"];
                dwData["ZUAUFPOS"] = dwGAAUF["GAAUFPOS"];
                SResult = INTEXDBC.QueryZUTEI(dwData, 1);
                SetMaster.Tables["ZUTEI"].Clear();
                SetMaster.Merge(SResult);
                if (SetMaster.Tables["ZUTEI"].Rows.Count > 1)
                    return;
                #region====获取该原料的总需求/库存/锁定=======
                dwData["GAMANDANT"] = dwGAAUF["GAMANDANT"];
                dwData["GAART"] = dwGAAUF["GAART"];
                dwData["GAFARBE"] = dwGAAUF["GAFARBE"];
                dwData["C20"] = "RS";
                SResult = INTEXDBC.QueryGAAUF(dwData, 12);//库存
                if (SResult.Tables.Count == 0)
                    return;
                SResult.Tables[0].TableName = "GAAUF";
                if (SetMaster.Tables.Contains("GAAUF"))
                {
                    SetMaster.Tables["GAAUF"].Clear();
                }
                SetMaster.Merge(SResult);
                string part = dwGAAUF["GABEM"].ToString().Trim();

                SResult = INTEXDBC.QueryGAAUF(dwData, 13);//获取已经留位的锁定数量
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    var query = from DataRow ZU in SetMaster.Tables["GAAUF"].AsEnumerable()
                                where ZU["GAART"].ToString() == row["GAART"].ToString()
                                && ZU["GAFARBE"].ToString() == row["GAFARBE"].ToString()
                                && ZU["GPSPPART"].ToString() == row["GPSPPART"].ToString()
                                && ZU["GPFBPART"].ToString() == row["GPFBPART"].ToString()
                                && ZU["LO"].ToString() == row["LO"].ToString()
                                && ZU["LB"].ToString() == row["LB"].ToString()
                                && ZU["GLAUFMSL"].ToString() == row["GLAUFMSL"].ToString()

                                select ZU;
                    foreach (DataRow ZU in query)
                    {
                        double NETKG = Convert.ToDouble(ZU["NETKG"].ToString());
                        double locked = Convert.ToDouble(ZU["QLOCK"].ToString());
                        double nLock = Convert.ToDouble(row["QLOCK"].ToString());
                        double QISSUE = Convert.ToDouble(row["QISSUE"].ToString());
                        ZU["QLOCK"] = Math.Ceiling(locked + nLock - QISSUE);
                        ZU["QFREE"] = Math.Floor(NETKG - (locked + nLock - QISSUE));
                    }
                }

                SResult = INTEXDBC.QueryGAAUF(dwGAAUF, 14);//获取纺批预计的锁定数量
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    string GABEM = row["GABEM"].ToString().Trim();
                    if (GABEM == "")
                        continue;
                    var query = from DataRow ZU in SetMaster.Tables["GAAUF"].AsEnumerable()
                                where ZU["GAART"].ToString() == row["GAART"].ToString()
                                && ZU["GAFARBE"].ToString() == row["GAFARBE"].ToString()
                                && ZU["GLAUFMSL"].ToString() == row["GAAUFMSL"].ToString()
                                select ZU;
                    //找到任意一条对应的记录，就跳出循环。
                    foreach (DataRow ZU in query)
                    {
                        if (ZU["GPSPPART"].ToString().Trim() == GABEM
                            || ZU["GPFBPART"].ToString().Trim() == GABEM
                            )
                        {
                            double NETKG = Convert.ToDouble(ZU["NETKG"].ToString());
                            double locked = Convert.ToDouble(ZU["QLOCK"].ToString());
                            double QPLAN = Convert.ToDouble(ZU["QPLAN"].ToString());
                            double nLock = Convert.ToDouble(row["QPLAN"].ToString());
                            ZU["QPLAN"] = Math.Ceiling(QPLAN + nLock);
                            ZU["QFREE"] = Math.Floor(NETKG - (locked + nLock + QPLAN));
                            break;
                        }
                    }
                }
                /*
                if (vwYARNA.View != null)
                {
                    vwYARNA.View.MoveCurrentToFirst();
                }*/
                #endregion
                double ABPSTKLG = Convert.ToDouble(dwGAAUF["ABPSTKLG"].ToString());
                if (ABPSTKLG == 0)
                {
                    TargetRatio = 1;
                }
                else
                {
                    TargetRatio = ABPSTKLG / 10000000;
                }
                return;
                txtLSQty.Text = "0";
                txtLSDD.Text = "0";
                string YART = dwGAAUF["GAART"].ToString();
                string YVAR = dwGAAUF["GAVAR"].ToString();
                string YCOLOR = dwGAAUF["GAFARBE"].ToString();
                string filter = "ABAAUFKRS='LS' AND ABAART='" + YART + "' AND ABAVAR='" + YVAR + "' AND ABAFARBE='" + YCOLOR + "'";
                DataRow[] rows = SetMaster.Tables["ICC_ABAX"].Select(filter);
                if (rows.Length > 0)
                {
                    MessageBox.Show(YART + YVAR + YCOLOR + "已经创建了领纱单!");
                    return;
                }

                filter = "GAART='" + YART + "' AND GAVAR='" + YVAR + "' AND GAFARBE='" + YCOLOR + "'";
                rows = SetMaster.Tables["GAAUF"].Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "GAAUF";
                    SetDetail.Clear();
                    SetDetail.Merge(tb);
                    double qty = 0;
                    string dd = "";
                    foreach (DataRow row in tb.Rows)
                    {
                        qty += Convert.ToDouble(row["GAKG1"].ToString()) / 100;
                    }
                    txtLSQty.Text = qty.ToString();
                    txtLSDD.Text = vm.date;
                }

                if (vwTGAAUF.View != null)
                {
                    vwTGAAUF.View.MoveCurrentTo(-1);
                    vwTGAAUF.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwYARNA_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwYARNA == null)
                {
                    return;
                }
                if (dwYARNA["GAART"].ToString().StartsWith("2"))
                {
                    cmbLot.Text = "染批";
                }
                if (dwYARNA["GAART"].ToString().StartsWith("1"))
                {
                    cmbLot.Text = "纺批";
                    if (!dwYARNA["GLAUFMSL"].ToString().StartsWith("SM"))
                    {
                        cmbLot.Text = "染批";
                    }
                }

                DataSet SResult = INTEXDBC.QueryGLLG(dwYARNA, 3);
                SetMaster.GLLG.Clear();
                SetMaster.Merge(SResult);

                SResult = INTEXDBC.QueryGAAUF(dwYARNA, 17);
                SResult.Tables[0].TableName = "ZUTEI";
                SetMaster.ZUTEI.Clear();
                SetMaster.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, "JLTAV95" + ex);
            }
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRow dwData = SetMaster.ICC_QUERYTB.NewRow();
                dwData["GAMANDANT"] = app.Mandant;
                dwData["ABKKDAUFTNR"] = ABKKDAUFTNR;
                DataSet SResult = INTEXDBC.QueryGAAUF(dwData, 7);
                SetEnv.Tables["GAAUF"].Clear();
                SetEnv.Merge(SResult);
                SetDetail.Merge(SResult);
                SetDetail.GAAUF.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, "JLTAV95" + ex);
            }
        }


        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            tb.Text = tb.Text.Trim();
        }
        private void SplinLostSet_Click(object sender, RoutedEventArgs e)
        {
            if (dwMU == null)
                return;
            string ABAID = dwMU["ABAID"].ToString();
            var qMU = from DataRow dwRow in SetDetail.Tables["ICC_ABAX"].AsEnumerable()
                      where dwRow["ABAID"].ToString() == ABAID
                      select dwRow;
            foreach (DataRow row in qMU)
            {
                row.ItemArray = dwMU.ItemArray;
                INTEXDBC.UpdateICC_ABAX(row, 1);
            }
            SetEnv.Tables["ICC_ABAX"].Clear();
            SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);
        }

        private void SplinLostSetWarp_Click(object sender, RoutedEventArgs e)
        {
            if (dwMU == null)
                return;
            string SpinnLot = dwMU["SPINLOT"].ToString();
            string SELLER = dwMU["SELLERNAME"].ToString();
            var qMU = from DataRow dwRow in SetEnv.Tables["ICC_ABAX"].AsEnumerable()
                      where (dwRow["ABPWAKZ"].ToString() == "W"
                      || dwRow["ABPWAKZ"].ToString() == "K")
                      && dwRow["ABAWAKZ"].ToString() == "G"
                      select dwRow;
            foreach (DataRow dwRow in qMU)
            {
                string krs = dwRow["ABAAUFKRS"].ToString();
                string lfd = dwRow["ABAAUFLFD"].ToString();
                string pos = dwRow["ABAAUFPOS"].ToString();
                var query = from DataRow row in SetDetail.Tables["ICC_ABAX"].AsEnumerable()
                            where row["ABAVKAUFKRS"].ToString() == krs
                            && row["ABAVKAUFLFD"].ToString() == lfd
                            && row["ABAVKAUFPOS"].ToString() == pos
                            && row["ABAAUFTYP"].ToString() == "N"
                            select row;
                foreach (DataRow row in query)
                {
                    row["SPINLOT"] = dwMU["SPINLOT"];
                    row["SELLERNAME"] = dwMU["SELLERNAME"];
                    INTEXDBC.UpdateICC_ABAX(row, 1);
                }
            }
            SetEnv.Tables["ICC_ABAX"].Clear();
            SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);

        }

        private void SplinLostSetWeft_Click(object sender, RoutedEventArgs e)
        {
            if (dwMU == null)
                return;

            string SpinnLot = dwMU["SPINLOT"].ToString();
            string SELLER = dwMU["SELLERNAME"].ToString();
            var qMU = from DataRow dwRow in SetEnv.Tables["ICC_ABAX"].AsEnumerable()
                      where dwRow["ABPWAKZ"].ToString() == "R"
                      && dwRow["ABAWAKZ"].ToString() == "G"
                      select dwRow;
            foreach (DataRow dwRow in qMU)
            {
                string krs = dwRow["ABAAUFKRS"].ToString();
                string lfd = dwRow["ABAAUFLFD"].ToString();
                string pos = dwRow["ABAAUFPOS"].ToString();
                var query = from DataRow row in SetDetail.Tables["ICC_ABAX"].AsEnumerable()
                            where row["ABAVKAUFKRS"].ToString() == krs
                            && row["ABAVKAUFLFD"].ToString() == lfd
                            && row["ABAVKAUFPOS"].ToString() == pos
                            && row["ABAAUFTYP"].ToString() == "N"
                            select row;
                foreach (DataRow row in query)
                {
                    row["SPINLOT"] = dwMU["SPINLOT"];
                    row["SELLERNAME"] = dwMU["SELLERNAME"];
                    INTEXDBC.UpdateICC_ABAX(row, 1);
                }
            }
            SetEnv.Tables["ICC_ABAX"].Clear();
            SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);
        }

        private void SplinLostSetAll_Click(object sender, RoutedEventArgs e)
        {
            string SpinnLot = dwMU["SPINLOT"].ToString();
            string SELLER = dwMU["SELLERNAME"].ToString();
            var qMU = from DataRow dwRow in SetDetail.Tables["ICC_ABAX"].AsEnumerable()
                      where dwRow["ABAAUFTYP"].ToString() == "N"
                      select dwRow;
            foreach (DataRow dwRow in qMU)
            {
                dwRow["SPINLOT"] = dwMU["SPINLOT"];
                dwRow["SELLERNAME"] = dwMU["SELLERNAME"];
                INTEXDBC.UpdateICC_ABAX(dwRow, 1);
            }
            SetEnv.Tables["ICC_ABAX"].Clear();
            SetEnv.Merge(SetDetail.Tables["ICC_ABAX"]);
        }

        private void btnAllocate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGAAUF == null)
                {
                    MessageBox.Show("JLTAV95" + "请选择要保存的纱线!");
                    return;
                }
                if (dwGLLG == null)
                {
                    MessageBox.Show("JLTAV95" + "请选择要留位的记录!");
                    return;
                }

                string GLLBEHI = dwGLLG["GLLBEHI"].ToString();

                string GPBPARTIE = dwGLLG["GPBPARTIE"].ToString();
                string GPSPPART = dwGLLG["GPSPPART"].ToString();
                string GPFBPART = dwGLLG["GPFBPART"].ToString();
                string LPLBKZ = dwGLLG["LPLBKZ"].ToString();
                if (LPLBKZ.Trim() == "L")
                    LPLBKZ = "S";

                double GPBBSTKGZ = Convert.ToDouble(dwGLLG["GPBBSTKGZ"].ToString());

                double KG = Convert.ToDouble(dwGLLG["KG"].ToString());
                double FREEKG = Convert.ToDouble(dwGLLG["FREEKG"].ToString());
                double ALLOKG = Convert.ToDouble(dwGLLG["ALLOKG"].ToString());
                double GAKG1 = Convert.ToDouble(dwGAAUF["GAKG1"].ToString());
                double GAKG2 = Convert.ToDouble(dwGAAUF["GAKG2"].ToString());
                double GAKG3 = Convert.ToDouble(dwGAAUF["GAKG3"].ToString());
                double GAKG5 = Convert.ToDouble(dwGAAUF["GAKG5"].ToString());
                double REQ = System.Math.Round((GAKG1 - GAKG2 - GAKG3 - GAKG5) / 100, 3);
                if (REQ <= 0)
                    return;
                #region===dwZUTEI========
                DataRow dwZUTEI = INTEXDBC.Convert2ZUTEI(null, app.company, false);

                dwZUTEI["ZUMANDANT"] = dwGAAUF["GAMANDANT"];
                dwZUTEI["ZUAUFKRS"] = dwGAAUF["GAAUFKRS"];
                dwZUTEI["ZUAUFLFD"] = dwGAAUF["GAAUFLFD"];
                dwZUTEI["ZUAUFPOS"] = dwGAAUF["GAAUFPOS"];
                dwZUTEI["ZUSA"] = dwGAAUF["GASA"];
                dwZUTEI["ZUTEILNR"] = "0000";
                dwZUTEI["ZUPOS"] = dwGAAUF["GAPOS"];
                dwZUTEI["ZUART"] = dwGAAUF["GAART"];
                dwZUTEI["ZUVAR"] = dwGAAUF["GAVAR"];
                dwZUTEI["ZUFARBE"] = dwGAAUF["GAFARBE"];
                dwZUTEI["ZUNEUBEN"] = app.User;
                dwZUTEI["ZUNEUDAT"] = vm.date;

                dwZUTEI["ZUFLAG"] = LPLBKZ;

                dwZUTEI["ZUPARTIE"] = GPBPARTIE;
                dwZUTEI["ZUPARTIEKZ"] = dwGLLG["GPBPARTIEKZ"];
                dwZUTEI["ZULO"] = dwGLLG["GLLO"];
                string GPBLB = dwGLLG["GPBLB"].ToString();
                string GLLB = dwGLLG["GLLB"].ToString();
                if (GPBLB == "Y*")
                {
                    dwZUTEI["ZULB"] = GPBLB;
                }
                else
                {
                    dwZUTEI["ZULB"] = GLLB;
                }

                dwZUTEI["ZULBEHI"] = GLLBEHI;
                dwZUTEI["ZUSPPART"] = dwGLLG["GPSPPART"];
                #endregion
                #region====FREEKG > REQ======
                if (FREEKG <= REQ)
                {
                    //需求100，此箱50
                    REQ = FREEKG;
                }
                ALLOKG += REQ;
                FREEKG -= REQ;
                GPBBSTKGZ += REQ * 1000;
                dwGLLG["ALLOKG"] = ALLOKG;
                dwGLLG["FREEKG"] = FREEKG;
                dwGLLG["GPBBSTKGZ"] = GPBBSTKGZ;
                dwZUTEI["ZUEINTKG"] = REQ * 1000;

                if (LPLBKZ == "P")
                {
                    GAKG5 += REQ * 100;
                }
                else
                {
                    GAKG3 += REQ * 100;
                }
                dwGAAUF["GAKG3"] = GAKG3;
                dwGAAUF["GAKG5"] = GAKG5;
                string SPINLOT = "";
                string SELLERNAME = "";
                if (GPFBPART.Trim().Length < 3)
                {
                    SPINLOT = dwGLLG["GPSPPART"].ToString();
                }
                else
                {
                    SPINLOT = dwGLLG["GPSPPART"].ToString();
                }
                SELLERNAME = Helpers.Helper.Left(dwGLLG["GLBEM"].ToString(), 12);
                dwGAAUF["SELLERNAME"] = SELLERNAME;
                dwGAAUF["SPINLOT"] = SPINLOT;
                dwGAAUF["GABEM"] = Helpers.Helper.Left(SPINLOT + SELLERNAME.PadRight(40), 40);
                txtRestReq.Text = ((GAKG1 - GAKG2 - GAKG3 - GAKG5) / 100).ToString();
                #endregion
                INTEXDBC.SaveZUTEI(dwZUTEI, app.company);
                //SetData.Tables["ZUTEI_ITX"].Rows.Add().ItemArray = dwZUTEI.ItemArray;
                INTEXDBC.UpdateGPBST(dwGLLG, 2);
                INTEXDBC.UpdateGAAUF(dwGAAUF, 1);
                DataRow dwData = SetEnv.ICC_QUERYTB.NewRow();
                dwData["ABPMANDANT"] = dwGAAUF["GAMANDANT"];
                dwData["ABPAUFKRS"] = dwGAAUF["GAAUFKRS"];
                dwData["ABPAUFLFD"] = dwGAAUF["GAAUFLFD"];
                dwData["ABPAUFPOS"] = dwGAAUF["GAAUFPOS"];
                INTEXDBC.UpdateABPOS(dwData, 6);
                string part = dwGAAUF["GABEM"].ToString().Trim();
                //this.cvYARNA.CustomFilter = "GLAUFMSL='" + dwGAAUF["GAAUFMSL"].ToString() + "' AND (GPSPPART LIKE '" + part + "%' OR GPFBPART LIKE '" + part + "%' )";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRemoveAllocate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                GD.RemoveAllocation(dwGAAUF);
                double GAKG1 = Convert.ToDouble(dwGAAUF["GAKG1"].ToString());
                double GAKG2 = Convert.ToDouble(dwGAAUF["GAKG2"].ToString());

                double GAKG3 = Convert.ToDouble(dwGAAUF["GAKG3"].ToString());
                double GAKG5 = Convert.ToDouble(dwGAAUF["GAKG5"].ToString());
                txtRestReq.Text = ((GAKG1 - GAKG2 - GAKG3 - GAKG5) / 100).ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnSaveGAAUF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGAAUF == null)
                {
                    MessageBox.Show("请选择要保存的纱线");
                    return;
                }
                dwGAAUF["GAART"] = Helpers.Helper.Left(dwGAAUF["GAART"].ToString().PadRight(20), 20);
                dwGAAUF["GAVAR"] = Helpers.Helper.Left(dwGAAUF["GAVAR"].ToString().PadRight(3), 3);
                dwGAAUF["GAFARBE"] = Helpers.Helper.Left(dwGAAUF["GAFARBE"].ToString().PadRight(10), 10);
                if (dwGAAUF["GABEM"].ToString().Trim().Length < 2)
                {
                    dwGAAUF["GABEM"] = " ".PadRight(39);
                }
                dwGAAUF["GABEM"] = Helpers.Helper.Left(dwGAAUF["GABEM"].ToString().PadRight(40), 40);
                DataSet SResult = INTEXDBC.QueryGSCOLOR(dwGAAUF, 5);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("纱线不存在，请核对编码");
                    return;
                }

                if (dwYARNA != null)
                {
                    if (cmbLot.Text.Trim() == "")
                    {
                        MessageBox.Show("请选择批次类型");
                        return;
                    }
                    string GPSPPART = dwYARNA["GPSPPART"].ToString();
                    string GPFBPART = dwYARNA["GPFBPART"].ToString();
                    if (cmbLot.Text.Trim() == "纺批")
                    {
                        dwGAAUF["GABEM"] = Helpers.Helper.Left(GPSPPART.PadRight(40), 40);
                    }
                    else
                    {
                        dwGAAUF["GABEM"] = Helpers.Helper.Left(GPFBPART.PadRight(40), 40);
                    }
                }
                INTEXDBC.SaveGAAUF(dwGAAUF, "YGR");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void btnAddRatio_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGAAUF == null)
                {
                    MessageBox.Show("请选择需要增投的生产单");
                }

                double ABPSOLLMENGE5 = Convert.ToDouble(dwGAAUF["ABPSOLLMENGE5"].ToString());
                double ADDQTY = (System.Math.Round(ABPSOLLMENGE5 * TargetRatio) - ABPSOLLMENGE5) / 100;
                txtAddQty.Text = ADDQTY.ToString();
                Calculate_Click(null, null);
                btnAdjustGAAUF_Click(null, null);
                dwGAAUF["ABPSTKLG"] = Math.Ceiling(TargetRatio * 10000000);
                INTEXDBC.UpdateABPOS(dwGAAUF, 8);
                vwGAAUF.View.MoveCurrentTo(-1);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnAddQty_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGAAUF == null)
                {
                    MessageBox.Show("请选择需要增投的生产单");
                }
                double ABPSOLLMENGE5 = Convert.ToDouble(dwGAAUF["ABPSOLLMENGE5"].ToString());
                double ADDQTY = Convert.ToDouble(txtAddQty.Text.ToString());
                this.txtAddRatio.Text = (System.Math.Round(100 * ADDQTY / ABPSOLLMENGE5 + 1, 4)).ToString();
                btnAddRatio_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnAdjustGAAUF_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dwGAAUF["GAART"] = Helpers.Helper.Left(dwGAAUF["GAART"].ToString().PadRight(20), 20);
                dwGAAUF["GAVAR"] = Helpers.Helper.Left(dwGAAUF["GAVAR"].ToString().PadRight(3), 3);
                dwGAAUF["GAFARBE"] = Helpers.Helper.Left(dwGAAUF["GAFARBE"].ToString().PadRight(10), 10);
                dwGAAUF["GABEM"] = Helpers.Helper.Left(dwGAAUF["GABEM"].ToString().Trim().PadRight(40), 40);
                dwGAAUF["GAAUFMSL"] = Helpers.Helper.Left(dwGAAUF["GAAUFMSL"].ToString().Trim().PadRight(2), 2);

                DataSet SResult = INTEXDBC.QueryGSCOLOR(dwGAAUF, 5);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("纱线不存在，请核对编码");
                    return;
                }
                INTEXDBC.SaveGAAUF(dwGAAUF, "JLT");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void Calculate_Click(object sender, RoutedEventArgs e)
        {
            double Total = 0;
            //4.计算用量，GAKG1= ABPSOLLMENGE7*TargetRatio*GASP1,ABPSOLLMENGE5*ABPSTKLG
            //5.GABASMG2=GAKG1/ABPSOLLMENGE5
            double ABPQTY = Convert.ToDouble(dwGAAUF["ABPSOLLMENGE5"].ToString()) * 0.01;
            double GABASMG2 = 1;// Convert.ToDouble(dwGAAUF["GABASMG2"].ToString()) * 0.001 * 0.01;
            double MRatio = GABASMG2 * TargetRatio;
            double GAKG1 = Math.Round(ABPQTY * MRatio * 100);
            dwGAAUF["GABASMG2"] = Math.Round(GAKG1 / ABPQTY * 1000 * 100);
            dwGAAUF["GAKG1"] = GAKG1;
        }
        #endregion
        #region===领纱========
        private void btnSameGOrder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGAAUF == null)
                    return;
                string GABEM = dwGAAUF["GABEM"].ToString();
                if (GABEM.Trim() == "")
                    return;
                filter = "GABEM='" + GABEM + "' AND GAKG2=0 AND GAKG3>0";
                rows = SetEnv.GAAUF.Select(filter);
                if (rows.Length == 0)
                {
                    MessageBox.Show("JLTAV95" + "请先完成留位");
                    return;
                }
                vwGAAUF.View.MoveCurrentTo(-1);
                vwTZUTEI.View.MoveCurrentTo(-1);
                DataRow dwData = SetDetail.ICC_QUERYTB.NewRow();
                DataSet SResult;
                DataRow dwNew = null;
                foreach (DataRow row in rows)
                {
                    string _MANDANT = row["GAMANDANT"].ToString();
                    string _KRS = row["GAAUFKRS"].ToString();
                    string _LFD = row["GAAUFLFD"].ToString();
                    string _POS = row["GAAUFPOS"].ToString();
                    filter = "ZUAUFKRS='" + _KRS + "' AND ZUAUFLFD='" + _LFD + "' AND ZUAUFPOS='" + _POS + "'";
                    DataRow[] zux = SetDetail.ZUTEI.Select(filter);
                    foreach (DataRow zx in zux)
                    {
                        zx.Delete();
                    }
                    dwData["ZUMANDANT"] = _MANDANT;
                    dwData["ZUAUFKRS"] = _KRS;
                    dwData["ZUAUFLFD"] = _LFD;
                    dwData["ZUAUFPOS"] = _POS;
                    SResult = INTEXDBC.QueryZUTEI(dwData, 10);
                    if (SResult.Tables[0].Rows.Count == 0)
                        continue;
                    dwNew = SetDetail.GAAUF.Rows.Add();
                    dwNew.ItemArray = row.ItemArray;
                    SetDetail.Merge(SResult);

                    filter = "ABAVKAUFKRS='" + _KRS + "' AND ABAVKAUFLFD='" + _LFD + "' AND ABAVKAUFPOS='" + _POS + "'";
                    zux = SetDetail.ICC_ABAX.Select(filter);
                    foreach (DataRow zx in zux)
                    {
                        zx.Delete();
                    }
                    dwData["ABAMANDANT"] = _MANDANT;
                    dwData["ABAVKAUFKRS"] = _KRS;
                    dwData["ABAVKAUFLFD"] = _LFD;
                    dwData["ABAVKAUFPOS"] = _POS;
                    SResult = INTEXDBC.QueryICC_ABAX(dwData, 4);
                    SetDetail.Merge(SResult);
                    row.Delete();
                }
                SetDetail.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, "JLTAV95" + ex);
            }
        }
        #endregion

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataRow dwData;
                string _MANDANT = dwTGAAUF["GAMANDANT"].ToString();
                string _KRS = dwTGAAUF["GAAUFKRS"].ToString();
                string _LFD = dwTGAAUF["GAAUFLFD"].ToString();
                string _POS = dwTGAAUF["GAAUFPOS"].ToString();
                filter = "GAAUFKRS='" + _KRS + "' AND GAAUFLFD='" + _LFD + "' AND GAAUFPOS='" + _POS + "' AND GAPOS='" + dwTGAAUF["GAPOS"].ToString() + "' ";
                rows = SetDetail.GAAUF.Select(filter);
                vwTGAAUF.View.MoveCurrentTo(-1);
                vwTZUTEI.View.MoveCurrentTo(-1);
                foreach (DataRow row in rows)
                {
                    filter = "ZUAUFKRS='" + _KRS + "' AND ZUAUFLFD='" + _LFD + "' AND ZUAUFPOS='" + _POS + "'";
                    DataRow[] zux = SetDetail.ZUTEI.Select(filter);
                    foreach (DataRow zx in zux)
                    {
                        zx.Delete();
                    }
                    filter = "ABAVKAUFKRS='" + _KRS + "' AND ABAVKAUFLFD='" + _LFD + "' AND ABAVKAUFPOS='" + _POS + "'";
                    zux = SetDetail.ICC_ABAX.Select(filter);
                    foreach (DataRow zx in zux)
                    {
                        zx.Delete();
                    }
                    dwData = SetEnv.GAAUF.Rows.Add();
                    dwData.ItemArray = row.ItemArray;
                    row.Delete();
                }
                SetDetail.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPreplan_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                DataSet SResult;
                SResult = vm.LSYarn(SetDetail);
                string path = @"C:\IntexReport\Data\JLTAV95";
                SResult.Tables["ABPOS"].TableName = "ABPOS_EXT";
                INTEXBL.Save2Xml(SResult, path);
                INTEXBL.AutoConvert(path, "JLTAB50MU");
                SetDetail.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, "JLTAV95" + ex);
            }
        }

        private void btnPrintLS_Click(object sender, RoutedEventArgs e)
        {
            dwABPOS = SetEnv.ICC_QUERYTB.NewRow();

            dwABPOS["ABKKDAUFTNR"] = KRS + "-" + Helpers.Helper.Right(LFD, 4);
            MessageInfo msgs = new MessageInfo();
            msgs.Where = app.User;
            msgs.What = "染色生产单打印";
            msgs.Details = "领纱单";
            INTEXBL.CheckEvents("A901301", dwABPOS, "领纱单", msgs);
        }

        private void btnBook_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwGAAUF == null)
                {
                    MessageBox.Show("JLTAV95" + "请选择要保存的纱线!");
                    return;
                }
                dwGAAUF["SELLERNAME"] = dwGAAUF["SELLERNAME"];
                dwGAAUF["SPINLOT"] = Helpers.Helper.Left(dwGAAUF["SPINLOT"].ToString().PadRight(10), 10);
                dwGAAUF["GABEM"] = Helpers.Helper.Left(dwGAAUF["SPINLOT"].ToString() + dwGAAUF["SELLERNAME"].ToString().PadRight(40), 40);
                INTEXDBC.UpdateGAAUF(dwGAAUF, 17);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
